// components/PFEP_components/pfep_register_dialog/pfep_register_dialog.js
const mybehavior = require("../../../behaviors/requests")
Component({
  behaviors: [mybehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    phone_num: "",
    password: "",
    password1: '',
    verification_code: '',
    base_url: 'https://logisticsengineering.gree.com',
    showId: true,
    time: 60,
    beforeClose: {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onBeforeClose(action) {
      return new Promise((resolve) => {
        if (action == 'confirm') {
          resolve(true)
        } else {
          resolve(true)
        }
      })
    },
    getIdCode() {
      let tel = this.data.phone_num;
      console.log(tel)
      if (tel) {
        let telnum = Number(tel);
        let myreg = /^[1][3,4,5,6,7,8][0-9]{9}$/;
        if (myreg.test(telnum)) {
          //开始获取验证码
          let url = this.data.base_url + "/pfep_api/login_manage/get_verification_code"
          // 后续ID需要更改为从缓存中获取
          let data = {
            ID: "984009",
            phone_num: telnum
          }
          this.post(url, data).then(res => {
            if (res.code == 20000) {
              this.setData({
                showId: false
              })
              this.timed()
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'error'
              })
            }
          }).catch(err => {
            if (err.msg) {
              wx.showToast({
                title: err.msg,
                icon: 'none'
              })
            } else {
              wx.showToast({
                title: "服务器出错了",
                icon: 'none'
              })
            }
          })
        } else {
          wx.showToast({
            title: '电话号码格式有误',
            icon: 'none'
          })
        }
      } else {
        wx.showToast({
          title: '请输入电话号码',
          icon: "error"
        })
      }
    },
    timed() {
      let timer = setInterval(() => {
        this.setData({
          time: this.data.time
        })
        if (this.data.time == 0) {
          clearInterval(timer);
          this.setData({
            time: 60,
            showId: true
          })
        }
      }, 1000)
    },
  },
  lifetimes: {
    attached() {
      var that = this
      that.setData({
        beforeClose: (action) => this.onBeforeClose(action)
      })
    }
  }
})